package listeners

import (
	"blockchains-aio/util"
	"fmt"
	"github.com/gin-gonic/gin"
)

func RegisterSwarm(router *gin.Engine) {
	swarm := router.Group("/swarm")
	{
		swarm.GET("/joinas/:manager", getJoinAs)
	}

}

func getJoinAs(ctx *gin.Context) {
	manager := ctx.Param("manager")
	out, err := util.DockerSwarmJoinToken(manager)
	if err != nil {
		ctx.JSON(400, fmt.Sprintf("Failed to get join token for %s: %v", manager, err))
		return
	}

	ctx.JSON(200, string(out))
}
